package com.market.member.service.impl;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.factory.PushApiFactory;
import com.block.datapush.model.result.PushApiResult;
import com.block.db.entity.*;
import com.block.db.mapper.AddressInfoMapper;
import com.block.db.mapper.MemberSourceInfoMapper;
import com.block.db.mapper.MerchantCheckInfoMapper;
import com.block.match.service.IMatchHandleService;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.IMerchantDataPushInfoService;
import com.market.member.service.IMemberSourceInfoService;
import com.market.member.service.IMerchantCheckInfoService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class MerchantCheckInfoServiceImpl extends ServiceImpl<MerchantCheckInfoMapper, MerchantCheckInfo> implements IMerchantCheckInfoService {

    private static final Logger log = LogFactory.getCommLog();

    @Autowired
    private MemberSourceInfoMapper memberSourceInfoMapper;
    @Autowired
    private AddressInfoMapper addressInfoMapper;
    @Autowired
    IMatchHandleService matchHandleService;
    @Autowired
    private MerchantCheckInfoMapper merchantCheckInfoMapper;
    @Autowired
    IMerchantBaseInfoService  merchantBaseInfoService;
    @Autowired
    IMemberSourceInfoService memberSourceInfoService;
    @Autowired
    IMerchantDataPushInfoService  merchantDataPushInfoService;


    /**
     * 测试撞库境外电话
     * @param merchantCheckInfo 入参
     */
    @Override
    public ResponseVo testCheck(MerchantCheckInfo merchantCheckInfo) throws Exception {
        //测试用户数据
        String uid = "1871156854654963713";
        Integer infoId = merchantCheckInfo.getInfoId();
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getById(infoId);
        PushApiKeyParam pushApiKeyParam = this.getPushKeyInfo(merchantBaseInfo);

        String mobileMd5 = SecureUtil.md5(merchantCheckInfo.getMobile());
        String city = merchantCheckInfo.getCity();
        AddressInfo addressInfo = addressInfoMapper.selectEntityByCityName(city);
        String infoName = merchantBaseInfoService.getInfoNameById(merchantCheckInfo.getInfoId());
        MemberSourceInfo memberSourceInfo = new MemberSourceInfo();
        memberSourceInfo.setUid(Long.valueOf(uid));
        memberSourceInfo.setMobileMd5(mobileMd5);
        memberSourceInfo.setIdcard(merchantCheckInfo.getIdCard());
        memberSourceInfo.setRealname(merchantCheckInfo.getRealName());
        memberSourceInfo.setMobile(merchantCheckInfo.getMobile());
        memberSourceInfo.setCity(addressInfo.getCity());
        memberSourceInfo.setProvince(addressInfo.getProvince());
        memberSourceInfo.setCityCode(addressInfo.getCityCode());
        memberSourceInfo.setProvinceCode(addressInfo.getProvinceCode());
        memberSourceInfo.setMobileMask(merchantCheckInfo.getMobile().substring(0,8));

        String orderNo = RandomUtil.randomString(32);
        JSONObject extJson = new JSONObject();

        int i = memberSourceInfoMapper.updateById(memberSourceInfo);
        MemberSourceInfo sourceInfo = memberSourceInfoService.getById(uid);

        if(i > 0){
//            ResponseVo responseVo = matchHandleService.PushApBManual(String.valueOf(merchantCheckInfo.getInfoId()), uid);
//            //测试数据只有撞库状态为001的时候才是真正的撞库成功,只要不是撞库成功的 则表示实际撞库失败
//            String message = responseVo.getRspMsg();
//            merchantCheckInfo.setMessage(message);
//            if (StrUtil.equals("0001",responseVo.getRspCode())){
//                merchantCheckInfo.setMessage("撞库成功");
//            }
//            merchantCheckInfo.setInfoName(infoName);
//            merchantCheckInfoMapper.insert(merchantCheckInfo);
//            return new ResponseVo(AppRspCodeConstant.SUCCESS);

            // 进行撞库操作
            PushApiResult pushApiResult = PushApiFactory.getService(merchantBaseInfo.getFactoryType())
                    .checkUser(orderNo, pushApiKeyParam, sourceInfo, extJson);
            log.info("【全流程撞库】撞库结束【infoId】{}【infoName】{}【result】{}",
                    pushApiKeyParam.getInfoId(), pushApiKeyParam.getInfoName(), JSONObject.toJSONString(pushApiResult));
            String message = pushApiResult.getMessage();
            merchantCheckInfo.setMessage(message);
            merchantCheckInfo.setInfoName(infoName);
            merchantCheckInfoMapper.insert(merchantCheckInfo);
            return new ResponseVo(AppRspCodeConstant.SUCCESS);
        }
        return new ResponseVo(AppRspCodeConstant.CHECK_SIGN_ERROR);
    }


    private PushApiKeyParam getPushKeyInfo(MerchantBaseInfo merchantBaseInfo){
        PushApiKeyParam pushApiKeyParam = new PushApiKeyParam();
        pushApiKeyParam.setInfoId(String.valueOf(merchantBaseInfo.getId()));
        pushApiKeyParam.setInfoName(merchantBaseInfo.getInfoName());
        pushApiKeyParam.setApiCode(merchantBaseInfo.getApiCode());
        pushApiKeyParam.setApiKey(merchantBaseInfo.getApiKey());
        pushApiKeyParam.setApiSecret(merchantBaseInfo.getApiSecret());
        pushApiKeyParam.setPubKey(merchantBaseInfo.getApiPubKey());
        pushApiKeyParam.setPriKey(merchantBaseInfo.getApiPriKey());
        pushApiKeyParam.setCheckUrl(merchantBaseInfo.getApiCheckUrl());
        pushApiKeyParam.setRegisterUrl(merchantBaseInfo.getApiRegisterUrl());
        pushApiKeyParam.setQueryUrl(merchantBaseInfo.getApiQueryUrl());
        pushApiKeyParam.setProtocolUrl(merchantBaseInfo.getApiProtocolUrl());
        return pushApiKeyParam;
    }
}
